package OpenID::Tester::Test::CheckID;
use strict;
use base 'OpenID::Tester::Test';

sub needs_op { 1 }
sub needs_rp { 1 }

sub tests {
    my ($class, $tester) = @_;
    my @tests;
    foreach my $version ("1.1", "2.0", "1.1,2.0") {
        foreach my $assoc_mode ("stateful_new", "stateless") {
            push @tests, $class->new($tester,
                                     version => $version,
                                     assoc_mode => $assoc_mode,
                                     );
        }
    }
    return @tests;
}

sub init {
    my ($self, %args) = @_;
    $self->{version} = $args{version};
    $self->{assoc_mode} = $args{assoc_mode};
}

sub des {
    "Checks the check_id mode.  This is the main test.";
}

sub summary {
    my $self = shift;
    return "v=$self->{version} assoc=$self->{assoc_mode}";
}

sub run {
    my $self = shift;
    my $op_base = $self->tester->op_base;
    my $rp_base = $self->tester->rp_base;
    my $identity = $op_base . "/$self->{version}/identity/will-sign";
    my $check_url = $self->gen_url($rp_base . "/$self->{version}/rp",
                                   openid_identifier => $identity,
                                   assoc_mode => $self->{assoc_mode},
                                   );
    $self->status("Checking URL: $check_url");
    my $ua = $self->tester->ua;
    my $res = $ua->get($check_url);
    die "Response not successful: " . $res->status_line unless $res->is_success;
    die "Content is: " . $res->content unless $res->content =~ /^OK\b/;
    return 1;
}

1;
